﻿using System;
using Fly.Data;

namespace FlyWebFrame.Model
{
    public class News :BaseModel
    {
        public News()
        {
            this.key = "id";
            this.table = "news";
        }
        public int id;
        public string title;
        public string image;
        public string description;
        public string detail;
        public string attachment;
        public string viTitle;
        public string viDescription;
        public string viDetail;
        public string viAttachment;
        public DateTime since;
        public DateTime eventTime;
        public int catId;
        public int type;
        public int status;
        public int isFeatured;
        public string Date;
        public string catName;

        public string parentTitle;

        public Result Get(int page, int size, int type)
        {
            this.type = type;
            return this.Select("[status]=1 AND [type]=@type", null, "id DESC", null, page, size);
        }

        public Result GetFeatured()
        {
            return this.Select("[status]=1 AND isFeatured = 1 AND type = 1", null, "id DESC", null, 0, 3);
        }

        public Result GetDisableNews(int page, int size)
        {
            return this.Select("[status]=0", null, "id DESC", null, page, size);
        }

        public int CountPageDisable(int size, int type)
        {
            this.type = type;
            int total = (int)this.Count("[status]=0").Data;
            int page = total / size;
            if (total % size != 0)
            {
                page++;
            }
            return page;
        }

        public int CountPage(int size, int type)
        {
            this.type = type;
            int total = (int)this.Count("[status]=1 AND type=@type").Data;
            int page = total / size;
            if (total % size != 0)
            {
                page++;
            }
            return page;
        }
        
        public Result GetByBigCat(string subcatIds, int page, int size)
        {
            return this.Select("catId in (" + subcatIds + ") AND [status]=1", null, "id DESC", null, page, size);
        }

        public Result GetByDisabledBigCat(string subcatIds, int page, int size)
        {
            return this.Select("catId in (" + subcatIds + ") AND [status]=0", null, "id DESC", null, page, size);
        }

        public Result GetByCat(int catId, int page, int size) {
            this.catId = catId;
            return this.Select("catId = @catId AND [status]=1", null, "id DESC", null, page, size);
        }

        public int CountPage(string subcatIds, int size)
        {
            int total = (int)this.Count("catId in (" + subcatIds + ") AND [status]=1").Data;
            int page = total / size;
            if (total % size != 0)
            {
                page++;
            }
            return page;
        }
        public Result GetOther(int CatId, int Id)
        {
            this.catId = CatId;
            this.id = Id;
            return this.Select("catId = @catId AND id <> @id AND [status]=1", null, "id DESC", null,0,20);
        }
        public Result GetBySubcat(int subCatId, int page, int size)
        {
            this.catId = subCatId;
            return this.Select("catId = @catId AND [status]=1",null, "id DESC", null, page, size);
        }

        public Result GetBySubcat(int SubCatId)
        {
            this.catId = SubCatId;
            return this.Select("catId = @catId AND [status]=1", "id,catId,", "id DESC",null);
        }

        public Result Search(string key, int page, int size)
        {
            this.title = "%" + key + "%";
            return this.Select("title like @title AND [status]=1", "id,title,since,viTitle", "id DESC", null, page, size);
        }

        public int CountPageSearch(string key, int size)
        {
            this.title = "%" + key + "%";
            int total = (int)this.Count("title like @title AND [status]=1").Data;
            int page = total / size;
            if (total % size != 0)
            {
                page++;
            }
            return page;
        }
        
        public bool Add()
        {
            if (attachment == null)
                attachment = "";
            if (viAttachment == null)
                viAttachment = "";
            this.since = DateTime.Now;
            this.status = 1;
            this.type = 1;
            this.isFeatured = 1;
            if(this.image != null && this.image != "")
                return this.IdentifyInsert("catId,title,image,description,detail,viTitle,viDescription,viDetail,since,status,type,isFeatured,viAttachment,attachment,eventTime").Status;
            else
                return this.IdentifyInsert("catId,title,description,detail,viTitle,viDescription,viDetail,since,status,type,isFeatured,viAttachment,attachment,eventTime").Status;
        }

        public bool EditDetail()
        {
            this.since = DateTime.Now;
            return this.Update("detail,viDetail").Status;
        }
        
        public Result Edit()
        {
            this.since = DateTime.Now;
            if (attachment == null)
                attachment = "";
            if (viAttachment == null)
                viAttachment = "";
            if (this.image == null || this.image == "")
            {
                return this.Update("catId,title,description,detail,viTitle,viDescription,viDetail,since,status,isFeatured,attachment,viAttachment,eventTime");
            }
            return this.Update("catId,image,title,description,detail,viTitle,viDescription,viDetail,since,status,isFeatured,attachment,viAttachment,eventTime");
        }
        public bool EditOnSubcatDelete(int SubcatId)
        {
            this.catId = 0;
            this.status = 0;          
            return this.Update("catId,status", "catId = " + SubcatId).Status;
        }
        public Result GetUpcomingCourses() {
            return this.Select("catId = 20",null,null,null,0,3);
        }
    }
}